package com.back.campus_back.controller;


import com.back.campus_back.domain.Carousel;
import com.back.campus_back.domain.Goods;
import com.back.campus_back.domain.GoodsCategory;
import com.back.campus_back.domain.User;
import com.back.campus_back.service.impl.CarouselServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-04-25
 */
@RestController
@RequestMapping("/carousel")
@Api(tags = "首页轮播图控制类")
public class CarouselController {
    @Autowired
    CarouselServiceImpl carouselService;

    //查询轮播图
    @ApiOperation("查询全部轮播图")
    @GetMapping("/selectCarousel")
    public List<Carousel> selectCarousel(){
        return  carouselService.selectCarousel();
    }

    //根据id更新轮播图
    @PostMapping("/updateCarousel")
    public String updateCarousel(@RequestBody Carousel carousel){
         carouselService.updateCarousel(carousel);
        return carouselService.updateCarousel(carousel);
    }

    //根据id删除轮播图
    @GetMapping("/deleteCarousel/{carouselId}")
    public String deleteCarousel(@PathVariable("carouselId") Integer carouselId){
        String deleteCarousel = carouselService.deleteCarousel(carouselId);
        return deleteCarousel;
    }

    //根据id查询
    @GetMapping("/findById/{carouselId}")
    public Carousel findByCarousel(@PathVariable("carouselId")Integer carouselId){
        Carousel carouselById = carouselService.findCarouselById(carouselId);
        return carouselById;
    }

    //用户添加商品
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public Map<String, Object> uploadImg(
            @RequestParam(value = "file", required = false) MultipartFile file ) {
//        //获取传进来的对象，拿到所属的商品分类
//        int goodType = goods.getGoodsType();
//        QueryWrapper<GoodsCategory> wrapper=new QueryWrapper<>();
//        wrapper.eq("category_id",goodType);
//        GoodsCategory goodsCategory= goodsCategoryMapper.selectOne(wrapper);
//        //获取所对应的类型文件夹
//        String cateFileName=goodsCategory.getCategoryFile();

        //构建文件上传所要保存的"文件夹路径"--这里是相对路径，保存到项目根路径的文件夹下, 项目根路径下的目录
        String url="src/main/resources/static/imgs/";
        String path = new String(url);

        //这是存放的上传文件的文件夹
        File uploads = new File(path);

        // 如果文件夹不存在则创建
        if (!uploads.exists() && !uploads.isDirectory()) {
            uploads.mkdirs();
        }

        String fileName = file.getOriginalFilename(); //得到上传时的文件名。
        try {
            File newfile = new File(uploads.getAbsolutePath() + File.separator + fileName);
            // 将上传的文件写入指定文件.
            file.transferTo(newfile);
        } catch (Exception e) {

            e.printStackTrace();
        }
        // 将文件上传的路径给保存到数据库
        String tmp_path = "../imgs/"+fileName;


        Map<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.put("tmp_path",tmp_path);

        return resultMap;
    }


    //分页查询
    @GetMapping("/findAllCarousel/{current}/{size}")
    public IPage<Carousel> findAllCarousel(@PathVariable("current") Integer current, @PathVariable("size") Integer size){
        return carouselService.findByCarousel(current,size);
    }



}

